package com.bosssoft.goods.controller;

import com.bosssoft.base.entity.Result;
import com.bosssoft.goods.domain.vo.BatchGoodsVO;
import com.bosssoft.goods.domain.vo.GoodsSearchQueryVO;
import com.bosssoft.goods.service.GoodsSearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 商品搜索api接口
 * @author Fang_Sir
 * @date 2021-04-20
 */
@RestController
@RequestMapping("/search")
@Api(tags = "5-商品查询接口")
public class GoodsSearchController {
    @Autowired
    private GoodsSearchService goodsSearchService;

    @GetMapping("/getGoodsAll")
    @ApiOperation(value = "获取所有商品信息")
    public Result<?> getGoodsAll(GoodsSearchQueryVO query,
                                 @RequestParam(required = false,defaultValue = "1") int page,
                                 @RequestParam(required = false,defaultValue = "10")int size){
        return Result.ok(goodsSearchService.getGoodsAll(query, page, size));
    }

    @GetMapping("/getGoodById/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品编号", required = true, dataType = "long", paramType = "path")
    })
    @ApiOperation(value = "通过id查询商品信息")
    public Result<?> getGoodsInfoById(@PathVariable("id") long id){
        return Result.ok(goodsSearchService.getGoodById(id));
    }

    /**
     * 通过名称查询商品
     * @return 返回商品信息
     */
    @GetMapping("/searchGoodsByName/{name}")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "name", value = "商品名称", required = true, dataType = "String", paramType = "path"),
        @ApiImplicitParam(name = "page", value = "查询页码", defaultValue = "1", dataType = "int"),
        @ApiImplicitParam(name = "size", value = "页大小", defaultValue = "10",dataType = "int")
    })
    @ApiOperation(value = "通过名称查询商品信息")
    public Result<?> searchGoodsByName(@PathVariable("name") String name,
                                       @RequestParam(required = false,defaultValue = "1") int page,
                                       @RequestParam(required = false,defaultValue = "20")int size){
        return Result.ok(goodsSearchService.searchGoodsByName(name,page,size));
    }

    @PostMapping("/getGoodsByGids")
    @ApiOperation(value = "通过编号批量查询商品信息")
    public Result<?> getGoodInfosWithGoodIds(@Valid @RequestBody BatchGoodsVO goods, BindingResult result){
        if(result.hasErrors()){
            return Result.error(result.getAllErrors().get(0).getDefaultMessage());
        }
        return Result.ok(goodsSearchService.getGoodsByGoodIds(goods.getGoodIds()));
    }
}
